<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注"/>
      </el-form-item>
      <el-form-item label="工单类型" prop="type">
        <el-select v-model="formData.type" placeholder="请选择工单类型">
          <el-option label="请选择字典生成" value=""/>
        </el-select>
      </el-form-item>
      <el-form-item label="积分" prop="score">
        <el-input v-model="formData.score" placeholder="请输入积分"/>
      </el-form-item>
      <el-form-item label="加分" prop="addScore">
        <el-input v-model="formData.addScore" placeholder="请输入加分"/>
      </el-form-item>
      <el-form-item label="明细数据" prop="details">
        <el-input v-model="formData.details" placeholder="请输入明细数据"/>
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="formData.productName" placeholder="请输入产品名称"/>
      </el-form-item>
      <el-form-item label="产品编码" prop="productCode">
        <el-input v-model="formData.productCode" placeholder="请输入产品编码"/>
      </el-form-item>
      <el-form-item label="派单人员" prop="sendUserId">
        <el-input v-model="formData.sendUserId" placeholder="请输入派单人员"/>
      </el-form-item>
      <el-form-item label="派单部门" prop="sendDeptId">
        <el-input v-model="formData.sendDeptId" placeholder="请输入派单部门"/>
      </el-form-item>
      <el-form-item label="发起预计处理完成时间" prop="sendEndDatetime">
        <el-date-picker
          v-model="formData.sendEndDatetime"
          type="date"
          value-format="x"
          placeholder="选择发起预计处理完成时间"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-input v-model="formData.state" placeholder="请输入状态"/>
      </el-form-item>
      <el-form-item label="优先级" prop="priority">
        <el-input v-model="formData.priority" placeholder="请输入优先级"/>
      </el-form-item>
      <el-form-item label="需求描述" prop="demandDesc">
        <el-input v-model="formData.demandDesc" placeholder="请输入需求描述"/>
      </el-form-item>
      <el-form-item label="需求图片" prop="demandImg">
        <el-input v-model="formData.demandImg" placeholder="请输入需求图片"/>
      </el-form-item>
      <el-form-item label="需求附件" prop="demandFile">
        <UploadFile v-model="formData.demandFile"/>
      </el-form-item>
      <el-form-item label="接收预计处理完成时间" prop="takeEndDatetime">
        <el-date-picker
          v-model="formData.takeEndDatetime"
          type="date"
          value-format="x"
          placeholder="选择接收预计处理完成时间"
        />
      </el-form-item>
      <el-form-item label="派单人员" prop="deliveryUserId">
        <el-input v-model="formData.deliveryUserId" placeholder="请输入派单人员"/>
      </el-form-item>
      <el-form-item label="派单时间" prop="deliveryDatetime">
        <el-date-picker
          v-model="formData.deliveryDatetime"
          type="date"
          value-format="x"
          placeholder="选择派单时间"
        />
      </el-form-item>
      <el-form-item label="接单时间" prop="takeDatetime">
        <el-date-picker
          v-model="formData.takeDatetime"
          type="date"
          value-format="x"
          placeholder="选择接单时间"
        />
      </el-form-item>
      <el-form-item label="接单人员" prop="takeUserId">
        <el-input v-model="formData.takeUserId" placeholder="请输入接单人员"/>
      </el-form-item>
      <el-form-item label="完成结果" prop="finishResult">
        <el-input v-model="formData.finishResult" placeholder="请输入完成结果"/>
      </el-form-item>
      <el-form-item label="完成图片" prop="finishImg">
        <el-input v-model="formData.finishImg" placeholder="请输入完成图片"/>
      </el-form-item>
      <el-form-item label="完成附件" prop="finishFile">
        <UploadFile v-model="formData.finishFile"/>
      </el-form-item>
      <el-form-item label="完成时间" prop="finishDatetime">
        <el-date-picker
          v-model="formData.finishDatetime"
          type="date"
          value-format="x"
          placeholder="选择完成时间"
        />
      </el-form-item>
      <el-form-item label="卡片实例id" prop="outTrackId">
        <el-input v-model="formData.outTrackId" placeholder="请输入卡片实例id"/>
      </el-form-item>
      <el-form-item label="审核结果" prop="auditResult">
        <el-input v-model="formData.auditResult" placeholder="请输入审核结果"/>
      </el-form-item>
      <el-form-item label="审核人" prop="auditUserId">
        <el-input v-model="formData.auditUserId" placeholder="请输入审核人"/>
      </el-form-item>
      <el-form-item label="审核备注" prop="auditRemark">
        <el-input v-model="formData.auditRemark" placeholder="请输入审核备注"/>
      </el-form-item>
      <el-form-item label="审核时间" prop="auditDatetime">
        <el-date-picker
          v-model="formData.auditDatetime"
          type="date"
          value-format="x"
          placeholder="选择审核时间"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import {VisualOrder, VisualOrderApi} from '@/api/business/design/visual'

/** 设计-视觉工单 表单 */
defineOptions({name: 'VisualOrderForm'})

const {t} = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  remark: undefined,
  type: undefined,
  score: undefined,
  addScore: undefined,
  details: undefined,
  productName: undefined,
  productCode: undefined,
  sendUserId: undefined,
  sendDeptId: undefined,
  sendEndDatetime: undefined,
  state: undefined,
  priority: undefined,
  demandDesc: undefined,
  demandImg: undefined,
  demandFile: undefined,
  takeEndDatetime: undefined,
  deliveryUserId: undefined,
  deliveryDatetime: undefined,
  takeDatetime: undefined,
  takeUserId: undefined,
  finishResult: undefined,
  finishImg: undefined,
  finishFile: undefined,
  finishDatetime: undefined,
  outTrackId: undefined,
  auditResult: undefined,
  auditUserId: undefined,
  auditRemark: undefined,
  auditDatetime: undefined
})
const formRules = reactive({
  score: [{required: true, message: '积分不能为空', trigger: 'blur'}]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await VisualOrderApi.getVisualOrder(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({open}) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as VisualOrder
    if (formType.value === 'create') {
      await VisualOrderApi.createVisualOrder(data)
      message.success(t('common.createSuccess'))
    } else {
      await VisualOrderApi.updateVisualOrder(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    remark: undefined,
    type: undefined,
    score: undefined,
    addScore: undefined,
    details: undefined,
    productName: undefined,
    productCode: undefined,
    sendUserId: undefined,
    sendDeptId: undefined,
    sendEndDatetime: undefined,
    state: undefined,
    priority: undefined,
    demandDesc: undefined,
    demandImg: undefined,
    demandFile: undefined,
    takeEndDatetime: undefined,
    deliveryUserId: undefined,
    deliveryDatetime: undefined,
    takeDatetime: undefined,
    takeUserId: undefined,
    finishResult: undefined,
    finishImg: undefined,
    finishFile: undefined,
    finishDatetime: undefined,
    outTrackId: undefined,
    auditResult: undefined,
    auditUserId: undefined,
    auditRemark: undefined,
    auditDatetime: undefined
  }
  formRef.value?.resetFields()
}
</script>
